package com.huadao.pda.base;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.common.RockToast;
import com.huadao.pda.common.UtilHelpers;
import com.huadao.pda.common.dialog.DialogFragmentHelper;
import com.huadao.pda.common.exception.ActivityNotAttachedException;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseDialogFragment extends DialogFragment implements BaseView {
    protected Context mContext;
    private Unbinder unbinder;
    protected View view;

    protected Toolbar toolbar;
    private View back;
    private View close;
    private TextView title;
    private BasePresenter basePresenter;

    //对话框占整个页面的大小,默认全屏
    private double widthSscale;
    private double heightScale;

    protected final String TAG = getClass().getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        scale=dialogScale();
        widthSscale = dialogWidthScale();
        widthSscale = widthSscale > 1 ? 1 : widthSscale;
        heightScale = dialogHeightScale();
        heightScale = heightScale > 1 ? 1 : heightScale;
//        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Light_NoActionBar);
        if (widthSscale == 1 && heightScale == 1) {
            setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme);
        } else {
            setStyle(DialogFragment.STYLE_NORMAL, R.style.Base_AlertDialog);
        }
        this.mContext = getActivity();
    }

//    protected double dialogScale() {
//        return 1;
//    }

    protected double dialogWidthScale() {
        return 1;
    }

    protected double dialogHeightScale() {
        return 1;
    }

    protected abstract BasePresenter getPresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getLayoutId(), container, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftInput(v);
            }
        });
//        view.setBackgroundResource(R.drawable.main_bg);
        unbinder = ButterKnife.bind(this, view);
        initActionBar();
        onInit();
        basePresenter = getPresenter();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (widthSscale != 1 || heightScale != 1) {
            Window window = getDialog().getWindow();
            if (window != null) {
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                WindowManager.LayoutParams params = window.getAttributes();
                params.height = (int) (BaseHelper.getScreenHeight(getActivity()) * heightScale - UtilHelpers.getStateBar());
                params.width = (int) (BaseHelper.getScreenWidth(getActivity()) * widthSscale);
                params.gravity = Gravity.CENTER;
                window.setAttributes(params);
            }
        } else {
            Window window = getDialog().getWindow();
            if (window != null) {
                WindowManager.LayoutParams params = window.getAttributes();
                params.height = BaseHelper.getScreenHeight(getActivity()) - UtilHelpers.getStateBar();
                params.width = BaseHelper.getScreenWidth(getActivity());
                params.gravity = Gravity.BOTTOM;
                window.setAttributes(params);
            }
        }
    }

        protected void initActionBar () {
            toolbar = view.findViewById(R.id.toolbar);
            if (toolbar != null) {
                back = view.findViewById(R.id.back);
                title = view.findViewById(R.id.title);
                close = view.findViewById(R.id.close);
                if (back != null) {
                    back.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onBack();
                        }
                    });
                }
                if (close != null) {
                    close.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClose();
                        }
                    });
                }
                ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
                ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
                if (actionBar != null) {
                    actionBar.setDisplayShowTitleEnabled(false);
                }
            }
        }

        private void onClose () {
            this.dismiss();
        }

        protected void onBack () {
            this.dismiss();
        }

        protected void setBackVisibility ( int visible){
            if (back == null) {
                return;
            }
            back.setVisibility(visible);
        }

        protected void setCloseVisibility ( int visible){
            if (close == null) {
                return;
            }
            close.setVisibility(visible);
        }

        protected void setTitle (String title){
            if (this.title != null) {
                this.title.setText(title);
            }
        }

        protected abstract int getLayoutId ();

        protected abstract void onInit ();

        protected void hideSoftInput (View view){
            IBinder token = view.getWindowToken();
            InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }

        @Override
        public void onDestroy () {
            unbinder.unbind();
//        if(EventBus.getDefault().isRegistered(this)){
//            EventBus.getDefault().unregister(this);
//        }
            if (basePresenter != null) {
                basePresenter.detachView();
                basePresenter = null;
            }
            super.onDestroy();
        }

        @Override
        public void showLoading (String msg){
            checkActivityAttached();
            ((BaseActivity) mContext).showLoading(msg);
        }

        @Override
        public void hideLoading () {
            checkActivityAttached();
            ((BaseActivity) mContext).hideLoading();
        }

        @Override
        public void showToast (String msg){
            checkActivityAttached();
            RockToast.showToastSafe(msg);
        }

        @Override
        public void showDialogMessage (String title, String msg){
            if (TextUtils.isEmpty(title)) {
                title = "";
            }
            DialogFragmentHelper.showMessageDialog(getActivity(), title, msg);
        }

        public void checkActivityAttached () {
            if (getActivity() == null) {
                throw new ActivityNotAttachedException();
            }
        }
    }
